package cn.ll.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ll.po.CurrencyRate;
import cn.ll.service.CurrencyRateService;
/**
 * 汇率都是1外币兑换多少人民币的形式
 * @author Administrator
 *
 */
public class CurrencyRateAction extends Action {
	
	public void save(HttpServletRequest request,HttpServletResponse response){
		String currency = request.getParameter("currency");
		String currencyDesc = request.getParameter("currency_desc");
		String rate = request.getParameter("rate");
		String remark = request.getParameter("remark");
		
		CurrencyRateService currencyRateService = (CurrencyRateService) this.getApplicationContext().getBean("currencyRateService");
		CurrencyRate currencyRate = new CurrencyRate();
//		currencyRate.setCurrency(currency);
//		currencyRate.setCurrencyDesc(currencyDesc);
//		currencyRate.setRate(Float.valueOf(rate));
//		currencyRate.setRemark(remark);
		currencyRate.setCurrency("加拿大元");
		currencyRate.setCurrencyDesc("加拿大元");
		currencyRate.setRate(Float.valueOf("6.58737"));
		currencyRateService.save(currencyRate);
		
		Map map = new java.util.HashMap(0);
		map.put("success", true);
		List list = new ArrayList();
		list.add(map);
		rtn(list, request, response);
		return;
	}
	
	/**
	 * 更新汇率
	 * @param request
	 * @param response
	 */
	public void update(HttpServletRequest request,HttpServletResponse response){
		String currency = request.getParameter("currency");
		String currencyDesc = request.getParameter("currency_desc");
		String rate = request.getParameter("rate");
		String remark = request.getParameter("remark");
		
		CurrencyRateService currencyRateService = (CurrencyRateService) this.getApplicationContext().getBean("currencyRateService");
		CurrencyRate currencyRate = currencyRateService.findByCurrency(currency);
		if(currencyRate != null){
			if(currencyDesc != null && currencyDesc.length() > 0){
				currencyRate.setCurrencyDesc(currencyDesc);
			}
			if(rate != null && rate.length() > 0){
				currencyRate.setRate(Float.valueOf(rate));
			}
			if(remark != null && remark.length() > 0){
				currencyRate.setRemark(remark);
			}
			
			currencyRateService.update(currencyRate);
			
			Map map = new java.util.HashMap(0);
			map.put("success", true);
			rtn(map, request, response);
			return;
		}
		else{
			Map map = new java.util.HashMap(0);
			map.put("success", false);
			rtn(map, request, response);
			return;
		}
	}
	
	public void findAll(HttpServletRequest request,HttpServletResponse response){
		CurrencyRateService currencyRateService = (CurrencyRateService) this.getApplicationContext().getBean("currencyRateService");
		List<CurrencyRate> list = currencyRateService.findAll();
		Map map = new java.util.HashMap(0);
		map.put("success", true);
		map.put("items", list);
		rtn(map, request, response);
		return;
	}
	
	public void readAllForSelect(HttpServletRequest request,HttpServletResponse response){
		CurrencyRateService currencyRateService = (CurrencyRateService) this.getApplicationContext().getBean("currencyRateService");
		List<Map> list = currencyRateService.readAllForSelect();
		Map map = new java.util.HashMap(0);
		map.put("success", true);
		map.put("items", list);
		rtn(map, request, response);
		return;
	}
}
